package cn.hy.transaction;

import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.CallbackPreferringPlatformTransactionManager;
import org.springframework.transaction.support.SimpleTransactionStatus;
import org.springframework.transaction.support.TransactionCallback;

@Component
public class MyTxPlatformTransactionManager implements CallbackPreferringPlatformTransactionManager {

    @Override
    public <T> T execute(TransactionDefinition definition, TransactionCallback<T> callback) throws TransactionException {
        System.out.println("i am enter ..");
        TransactionStatus transaction = getTransaction(definition);
        return callback.doInTransaction(transaction);
    }

    @Override
    public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
        return new SimpleTransactionStatus();
    }

    @Override
    public void commit(TransactionStatus status) throws TransactionException {
        if (status.isCompleted()) {
            System.out.println("commit");
        }

    }

    @Override
    public void rollback(TransactionStatus status) throws TransactionException {
        if(status.isRollbackOnly()) {
            System.out.println("Rollback");
        }
    }
}
